package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author qzh
 * @create 2020-01-10 2020/1/10
 */
@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {


    @Reference
    private OrderSettingService orderSettingService;
    @Reference
    private OrderService orderService;
    @Reference
    private MemberService memberService;


    @RequestMapping("upload")
    public Result upload(MultipartFile excelFile){

        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<OrderSetting> data = new ArrayList<>();
            for (String[] strings : list) {
                String date = strings[0];
                String number = strings[1];
                OrderSetting orderSetting = new OrderSetting(new Date(date), Integer.parseInt(number));
                data.add(orderSetting);

            }
            orderSettingService.add(data);
            
            
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }



    @RequestMapping("getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){

        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);

            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){

        try {
            orderSettingService.editNumberByDate(orderSetting);

            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map,Integer[] checkitemIds) throws Exception {
        Integer id = (Integer)map.get("id");
        //根据id删除预约信息
        orderService.deleteById(id);
        Integer memberId = (Integer)map.get("member_id");
        //根据用户id删除用户
        memberService.delete(memberId);
        Member member = new Member();
        member.setName((String) map.get("name"));
        String orderDate = (String) map.get("orderDate");
        /*  Date date = DateUtils.parseString2Date(orderDate);*/
        member.setOrderDate(orderDate);
        member.setName((String) map.get("name"));
        member.setIdCard((String) map.get("idCard"));
        member.setPhoneNumber((String) map.get("phoneNumber"));
        member.setSex((String) map.get("sex"));
       /* try {
            orderSettingService.editOrder(member,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑失败");
        }*/
        return orderSettingService.editOrder(member,checkitemIds);
        /*  return new Result(true,"编辑成功");*/



    }

    @RequestMapping("/delete")
    public Result delete(Integer id){


        try {
            orderService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
        return new Result(true, "删除成功");
    }
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map map = memberService.findMemberAndOrderByOrderId(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }



    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        return orderSettingService.pageQuery(queryPageBean);
    }

    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<Setmeal> list = orderSettingService.findAll();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @RequestMapping("add")
    public Result add(@RequestBody OrderMessage map, Integer[] setmealIds){
        try {
            orderSettingService.addOrderSetting(map,setmealIds);
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }





}
